package com.bishe.service;

import cn.dev33.satoken.stp.StpUtil;
import com.api.entitis.Admin;
import com.api.entitis.model.LoginModel;
import com.api.entitis.model.UserModel;
import com.api.service.IAdminService;
import com.api.util.UserStpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bishe.mapper.AdminMapper;
import org.springframework.stereotype.Service;

/**
 * 管理员接口实现
 * @date 2022/02/18 27:11:38
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {

    @Override
    public UserModel login(LoginModel loginModel) {
        String userName = loginModel.getUserName();
        String password = loginModel.getPassword();
        Boolean rememberMe = loginModel.getRememberMe();
        if (rememberMe == null){
            rememberMe = false;
        }
        //根据用户名密码查找
        Admin one = getOne(new LambdaQueryWrapper<Admin>().eq(Admin::getUserName, userName).eq(Admin::getPassword, password),false);
        if (one == null) {
            return null;
        }

        StpUtil.login(one.getId(),rememberMe);
        UserModel userModel = new UserModel();
        userModel.setId(one.getId());
        userModel.setName(one.getUserName());
        userModel.setType("admin");
        userModel.setToken(UserStpUtil.getTokenValue());
        return userModel;
    }
}
